#!/bin/sh

function GstIsNull {
	if [ -z $1 ]
	then
		echo "input your parameter"
		exit
	fi
	str=$(config status -s | grep $1)
	if [ -z "$str" ]
	then
		return 0
	elif [ -n "$str" ]
	then
		return 1
	fi
}

function RepoIsEmpty {
	case $1 in
		ibus)
			GstIsNull ibus;;
		vim)
			GstIsNull init.vim;;
		task)
			GstIsNull task;;
		zsh)
			GstIsNull zsh;;
		*)
			echo "Usage:RepoIsEmpty [ibus|vim|task|pkm]"
			exit;;
	esac
}

function ibus_bkp {
	if RepoIsEmpty ibus
	then
		echo "The Repository is empty"
	else
		config add ~/.config/ibus

		echo -n "Do you want to describe your commit [y/N]?"
		read -k 1 CONFIRM

		case $CONFIRM in
			Y|y)
				echo -n -e "\nInput your commit description:"
				read Des;;
			*)
				Des=1;;
		esac

		if [ $Des -eq 1 ]
		then
			config commit --quiet -m "auto backup ibus config($(date +%F))"
		else
			config commit --quiet -m "$Des"
		fi

		config push --quiet origin master &> /dev/null
		if [ $? -eq 0 ]
		then
			echo "Your ibus data backup complete"
		else
			echo "Something wrong with your ibus data backup"
		fi
	fi
}

function task_bkp {
	if RepoIsEmpty task
	then
		echo "The Repository is empty"
	else
		config add ~/.taskrc
		config add ~/.task
		echo -n "Do you want to describe your commit [y/N]?"
		read -k 1 CONFIRM

		case $CONFIRM in
			Y|y)
				echo -n -e "\nInput your commit description:"
				read Des;;
			*)
				Des=1;;
		esac

		if [ $Des -eq 1 ]
		then
			config commit --quiet -m "auto backup task config($(date +%F))"
		else
			config commit --quiet -m "$Des"
		fi

			config push --quiet origin master &> /dev/null
		if [ $? -eq 0 ]
		then
			echo "Your task data backup complete"
		else
			echo "Something wrong with your task data backup"
		fi
	fi
}

function fontconfig_bkp {
	config add ~/.config/fontconfig
	config commit -m "backup fontconfig ($(date +%F))"
	config push origin master
	echo "Your fontconfig data backup complete"
}

function vimconfig_bkp {
	config add ~/.config/nvim/init.vim
	config commit -m "backup fontconfig ($(date +%F))"
	config push origin master &> /dev/null
	echo "Your nvimconfig data backup complete"
}

function scripts_bkp {
	cd $HOME/.local/share/scripts
	git add -A
	git commit --quiet -m "scripts auto backup $(date +%F)"
	git push --quiet origin master &> /dev/null
	if [ $? -eq 0 ]
	then
		echo  "scripts backup complete"
	else
		echo "Something wrong with your scripts dir backup"
	fi
}

function mmps_bkp {
	cd $HOME/Documents/mindmap
	if clear_Repo $(pwd)
	then
		echo "the Repo is clear,nothing to be committed"
	else
		git add ./*
		git commit --quiet -m "mindmaps auto backup $(date +%F)"
		git push --quiet origin master &> /dev/null
		if [ $? -eq 0 ]
		then
			echo  "mindmaps backup complete"
		else
			echo "Something wrong with your mindmaps dir backup"
		fi
	fi
}
